﻿package com.google.zxing.client.android;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.shensoft.barcode.client.android.update.AndroidServiceXML;
import com.shensoft.barcode.client.android.update.DownLoadManager;
import com.shensoft.barcode.client.android.update.UpdataInfo;

public class UpdateActivity extends Activity {

	private final String TAG = this.getClass().getName();

	private final int UPDATA_NONEED = 0;
	private final int UPDATA_CLIENT = 1;
	private final int GET_UNDATAINFO_ERROR = 2;
	private final int SDCARD_NOMOUNTED = 3;
	private final int DOWN_ERROR = 4;
	private TextView dqtextView;
	private TextView fwqtextView;
	private Button getVersion;

	private UpdataInfo info;
	private String localVersion = "1.0";
	private String path;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update);
		getViews();
		setListeners();
	}

	// 获取页面元素对象
	private void getViews() {
		dqtextView = (TextView) findViewById(R.id.dqbanbenhao);
		fwqtextView = (TextView) findViewById(R.id.fwqbanbenhao);
		getVersion = (Button) findViewById(R.id.bbjiance);
		path = getResources().getString(R.string.url_server_xml);
		try {
			localVersion = getVersionName();
		} catch (Exception e) {
			Toast.makeText(UpdateActivity.this, "获取当前版本信息失败！！！",
					Toast.LENGTH_LONG).show();
		}
		dqtextView.setText("当前版本号：v" + localVersion);
	}

	// 按钮按下时监听
	private void setListeners() {
		getVersion.setOnClickListener(getVersionListener);
	}

	//
	private Button.OnClickListener getVersionListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			try {
				CheckVersionTask cv = new CheckVersionTask();
				new Thread(cv).start();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};

	/*
	 * 获取当前程序的版本号
	 */
	private String getVersionName() throws Exception {
		// 获取packagemanager的实例
		PackageManager packageManager = getPackageManager();
		// getPackageName()是你当前类的包名，0代表是获取版本信息
		PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
		return packInfo.versionName;
	}

	/*
	 * 从服务器获取xml解析并进行比对版本号
	 */
	public class CheckVersionTask implements Runnable {

		public void run() {
			try {
				info = AndroidServiceXML.getLastVideos(path);
				if (info == null) {
					// 待处理
					Message msg = new Message();
					msg.what = GET_UNDATAINFO_ERROR;
					handler.sendMessage(msg);
				} else {
					//fwqtextView.setText("v" + info.getVersion());
					if (info.getVersion().equals(localVersion)) {
						Log.i(TAG, "版本号相同无需升级");
						Message msg = new Message();
						msg.what = UPDATA_NONEED;
						handler.sendMessage(msg);
						// LoginMain();
					} else {
						Log.i(TAG, "版本号不同 ,提示用户升级 ");
						Message msg = new Message();
						msg.what = UPDATA_CLIENT;
						handler.sendMessage(msg);
					}					
				}
			} catch (Exception e) {
				// 待处理
				Message msg = new Message();
				msg.what = GET_UNDATAINFO_ERROR;
				handler.sendMessage(msg);
				e.printStackTrace();
			}			
		}
	}

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case UPDATA_NONEED:
				Toast.makeText(getApplicationContext(), "版本号相同无需升级",
						Toast.LENGTH_SHORT).show();
			case UPDATA_CLIENT:
				// 对话框通知用户升级程序
				Toast.makeText(getApplicationContext(), "可以升级程序啦~", 1).show();
				showUpdataDialog();
				break;
			case GET_UNDATAINFO_ERROR:
				// 服务器超时
				Toast.makeText(getApplicationContext(), "获取服务器更新信息失败", 1).show();
				// LoginMain();
				break;
			case SDCARD_NOMOUNTED:
				// sdcard不可用
				Toast.makeText(getApplicationContext(), "SD卡不可用", 1).show();
				break;
			case DOWN_ERROR:
				// 下载apk失败
				Toast.makeText(getApplicationContext(), "下载新版本失败", 1).show();
				// LoginMain();
				break;
			}
		}
	};

	/*
	 * 
	 * 弹出对话框通知用户更新程序
	 * 
	 * 弹出对话框的步骤： 1.创建alertDialog的builder. 2.要给builder设置属性, 对话框的内容,样式,按钮
	 * 3.通过builder 创建一个对话框 4.对话框show()出来
	 */
	protected void showUpdataDialog() {
		AlertDialog.Builder builer = new Builder(this);
		builer.setTitle("软件版本更新");
		builer.setMessage(info.getDescription());
		// 当点确定按钮时从服务器上下载 新的apk 然后安装
		builer.setPositiveButton("下载", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Log.i(TAG, "下载apk,更新");
				downLoadApk();
			}
		});
		// 当点取消按钮时进行登录
		builer.setNegativeButton("以后再说",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		AlertDialog dialog = builer.create();
		dialog.show();
	}

	/*
	 * 从服务器中下载APK
	 */
	protected void downLoadApk() {
		final ProgressDialog pd; // 进度条对话框
		pd = new ProgressDialog(UpdateActivity.this);
		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		pd.setMessage("正在下载更新~");
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			Message msg = new Message();
			msg.what = SDCARD_NOMOUNTED;
			handler.sendMessage(msg);
		} else {
			pd.show();
			new Thread() {
				@Override
				public void run() {
					try {
						Log.i(TAG, "进入下载方法");
						String downLoadUrl = info.getUrl();
						// 提取文件名称
						String apkName = getApkName(downLoadUrl);
						File file = DownLoadManager.getFileFromServer(
								downLoadUrl, pd, apkName);
						sleep(1000);
						installApk(file);
						pd.dismiss(); // 结束掉进度条对话框

					} catch (Exception e) {
						Message msg = new Message();
						msg.what = DOWN_ERROR;
						handler.sendMessage(msg);
						e.printStackTrace();
					}
				}
			}.start();
		}
	}

	private String getApkName(String url) {
		int last = url.lastIndexOf("/");
		return url.substring(last + 1, url.length());
	}

	// 安装apk
	protected void installApk(File file) {
		Log.i(TAG, "安装apk");
		Intent intent = new Intent();
		// 执行动作
		intent.setAction(Intent.ACTION_VIEW);
		// 执行的数据类型
		intent.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		startActivity(intent);
	}
}